/*
 * PaneGround.java
 *
 * Created on 21 novembre 2007, 23:09
 */
package bbos.Match.Views;

import bbos.Tools.bbTool;
import bbos.Match.Model.dMatch;
import bbos.Match.Model.dTeam;
import bbos.Match.Model.dPlayer;
import bbos.Match.Model.dSquareCollection;
import bbos.Match.Model.dSquare;
import bbos.*;
import bbos.Match.Model.Actions.daBlock;
import java.awt.*;
import java.awt.image.*;
import java.rmi.RemoteException;
import java.util.*;

/**
 *
 * @author  Administrateur
 */
public class vGround extends javax.swing.JPanel
{

    public static final double c_passRatio = 3.4;
    public static final int c_squareSize = 30;
    private Image ground = null;
    cGround pngListener;
    dMatch _model;

    /** Creates new form PaneGround */
    public vGround()
    {
        initComponents();
        pngListener = new cGround(_model);
        this.addMouseListener(pngListener);
        this.addMouseMotionListener(pngListener);
    }

    public void setModel(dMatch model)
    {
        _model = model;
        pngListener.setModel(_model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    void paintMeteo(Graphics g)
    {
        if (_model != null)
        {
           /* ground = _model.getMeteo().getMeteoPicture();
            /*
             * Draw field
             */
            int x = 0, y = 0;
            /*if (ground != null)
            {
                g.drawImage(ground, x, y, this);
            }*/
        }
    }

    void paintTeamLogo(Graphics g)
    {
        if (_model != null)
        {
            Graphics2D g2 = (Graphics2D) g;
            /*
             * Draw team logos
             */
            /*g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.4f));
            //Image logo_teamleft = getToolkit().getImage(getClass().getResource(_model.getLeftTeam().getTeamLogoUrl()));
            int width = logo_teamleft.getWidth(this);
            int heigth = logo_teamleft.getHeight(this);
            g2.drawImage(logo_teamleft, 196 - width / 2, 226 - heigth / 2, this);
            //Image logo_teamright = getToolkit().getImage(getClass().getResource(_model.getRightTeam().getTeamLogoUrl()));
            heigth = logo_teamright.getHeight(this);
            width = logo_teamright.getWidth(this);
            g2.drawImage(logo_teamright, 586 - width / 2, 226 - width / 2, this);
            g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 1.0f));*/
        }
    }

    void paintPassRange(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        /*
         * Pass range draw
         */
        if (_model != null)
        {
//            if (_model._selectedPlayer != null)
            {
               // if (_model._drawPass)
                {
                //    if (_model._onFlySquare != null)
                    {
/*                        g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.4f));
                        g2.setColor(_model._passRule._color);
                        g2.fillPolygon(_model._passRule.x_points, _model._passRule.y_points, 4);
                        g2.setColor(new Color(0, 0, 0));
                        g2.drawPolygon(_model._passRule.x_points, _model._passRule.y_points, 4);
                        g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 1.0f));*/
                    }
                }
            }
        }
    }

    void paintTeam(Graphics g, dTeam team)
    {
        Graphics2D g2 = (Graphics2D) g;
        if (_model != null)
        {
            /*try
            {*/
            for (int i = 0; i < team.getPlayers().size(); i++)
            {
                dPlayer player = (dPlayer) team.getPlayers().get(i);
                if (player.isOnThePitch())
                {
                    /*
                     * Draw icons
                     */
 /*                   Image photo;
                    if (player._isActive)
                    {
                        photo = player.getIconSelected();
                    }
                    else
                    {
                        photo = player.getIcon();
                    }
                    int width = photo.getWidth(this);
                    int heigth = photo.getHeight(this);
                    if (player._havePlayed)
                    {
                        g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.4f));
                    }
                    g2.drawImage(photo, player.getX() * c_squareSize + (c_squareSize - width) / 2, player.getY() * c_squareSize + (c_squareSize - heigth) / 2, this);
                    g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 1.0f));
                    Image state_image = getToolkit().getImage(getClass().getResource(player.getStateUrl()));
                    g.drawImage(state_image, player.getX() * c_squareSize, player.getY() * c_squareSize, this);
  * */
                }
            }
            /*} catch (RemoteException e) {
            System.err.println(e.getMessage());
        }*/
        }
    }

    void paintZone(dSquare s, Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        int nbTackleZone;
        switch (s.getState())
        {
            case dSquare.C_SQUARE_EMPTY:
                break;
            case dSquare.C_SQUARE_OPPONENT_INTERCEPT:
                //nbTackleZone = s.getTackleZone(_model._activeTeam);
                //paintSquare(s, g, nbTackleZone);
                break;
            case dSquare.C_SQUARE_OPPONENT_TO_BLOCK:
                g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.7f));
                switch (s.getBlockDices())
                {
                    case daBlock.DICE_ONE:
                        g2.setColor(new Color(20, 240, 20));
                        g2.fillRect(s.getX() * c_squareSize + 1 + 10, s.getY() * c_squareSize + 1 + 10, 10, 10);
                        g2.setColor(new Color(0, 0, 0));
                        g2.drawRect(s.getX() * c_squareSize + 1 + 10, s.getY() * c_squareSize + 1 + 10, 10, 10);
                        break;
                    case daBlock.DICE_TWO_FOR:
                        g2.setColor(new Color(20, 240, 20));
                        g2.fillRect(s.getX() * c_squareSize + 1 + 5, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.fillRect(s.getX() * c_squareSize + 1 + 15, s.getY() * c_squareSize + 1 + 15, 10, 10);
                        g2.setColor(new Color(0, 0, 0));
                        g2.drawRect(s.getX() * c_squareSize + 1 + 5, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.drawRect(s.getX() * c_squareSize + 1 + 15, s.getY() * c_squareSize + 1 + 15, 10, 10);
                        break;
                    case daBlock.DICE_TWO_AGAINST:
                        g2.setColor(new Color(240, 20, 20));
                        g2.fillRect(s.getX() * c_squareSize + 1 + 5, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.fillRect(s.getX() * c_squareSize + 1 + 15, s.getY() * c_squareSize + 1 + 15, 10, 10);
                        g2.setColor(new Color(0, 0, 0));
                        g2.drawRect(s.getX() * c_squareSize + 1 + 5, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.drawRect(s.getX() * c_squareSize + 1 + 15, s.getY() * c_squareSize + 1 + 15, 10, 10);
                        break;
                    case daBlock.DICE_THREE_FOR:
                        g2.setColor(new Color(240, 20, 20));
                        g2.fillRect(s.getX() * c_squareSize + 1 + 5, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.fillRect(s.getX() * c_squareSize + 1 + 15, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.fillRect(s.getX() * c_squareSize + 1 + 10, s.getY() * c_squareSize + 1 + 15, 10, 10);
                        g2.setColor(new Color(0, 0, 0));
                        g2.drawRect(s.getX() * c_squareSize + 1 + 5, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.drawRect(s.getX() * c_squareSize + 1 + 15, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.drawRect(s.getX() * c_squareSize + 1 + 10, s.getY() * c_squareSize + 1 + 10, 10, 10);
                        break;
                    case daBlock.DICE_THREE_AGAINST:
                        g2.setColor(new Color(20, 240, 20));
                        g2.fillRect(s.getX() * c_squareSize + 1 + 5, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.fillRect(s.getX() * c_squareSize + 1 + 15, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.fillRect(s.getX() * c_squareSize + 1 + 10, s.getY() * c_squareSize + 1 + 15, 10, 10);
                        g2.setColor(new Color(0, 0, 0));
                        g2.drawRect(s.getX() * c_squareSize + 1 + 5, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.drawRect(s.getX() * c_squareSize + 1 + 15, s.getY() * c_squareSize + 1 + 5, 10, 10);
                        g2.drawRect(s.getX() * c_squareSize + 1 + 10, s.getY() * c_squareSize + 1 + 10, 10, 10);
                        break;
                }
                g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 1.0f));
                break;
            case dSquare.C_SQUARE_PARTNER_TO_PASS:
                //nbTackleZone = s.getTackleZone(_model._opposingTeam);
                //paintSquare(s, g, nbTackleZone);
                break;
            case dSquare.C_SQUARE_PUSH_ZONE:
                paintSquare(s, g, 0);
                break;

            case dSquare.C_SQUARE_MOVE:
                //nbTackleZone = s.getTackleZone(_model._opposingTeam);
                //paintSquare(s, g, nbTackleZone);
                break;
            default:
                break;
        }

        switch (s.getBallState())
        {
            case dSquare.C_SQUARE_BALL_HAS_BALL:
                Image icon = (bbTool.getImage("/bbos/Images/ball.gif", "Ball", 10));
                int width = icon.getWidth(this);
                int heigth = icon.getHeight(this);
                g.drawImage(icon, s.getX() * c_squareSize + ((c_squareSize - width) / 2), s.getY() * c_squareSize + (c_squareSize - heigth) / 2, this);
                break;
            case dSquare.C_SQUARE_BALL_WAIT_BALL:
                g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.4f));
                Image icon2 = (bbTool.getImage("/bbos/Images/ball.gif", "Ball", 10));
                int width2 = icon2.getWidth(this);
                int heigth2 = icon2.getHeight(this);
                g.drawImage(icon2, s.getX() * c_squareSize + ((c_squareSize - width2) / 2), s.getY() * c_squareSize + (c_squareSize - heigth2) / 2, this);
                g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 1.0f));
                break;
        }
    }

    void paintSquare(dSquare s, Graphics g, int nbTackleZone)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.4f));
        switch (nbTackleZone)
        {
            case 0:
                g2.setColor(new Color(7, 119, 39));
                break;
            case 1:
                g2.setColor(new Color(237, 239, 27));
                break;
            case 2:
                /* Orange */
                g2.setColor(new Color(239, 147, 27));
                break;
            case 3:
                /* Red */
                g2.setColor(new Color(191, 15, 48));
                break;
            case 4:
                /* Light blue */
                g2.setColor(new Color(36, 215, 236));
                break;
            case 5:
                /* Blue */
                g2.setColor(new Color(36, 69, 236));
                break;
            case 6:
                /* Pink */
                g2.setColor(new Color(236, 36, 201));
                break;
            case 7:
                /* Violet */
                g2.setColor(new Color(163, 33, 97));
                break;
            default:
                /* Grey */
                g2.setColor(new Color(100, 100, 100));
                break;
        }
        if (!_model.isAPlayer(s))
        {
            g2.fillRect(s.getX() * c_squareSize + 1, s.getY() * c_squareSize + 1, 28, 28);
            if (s.isSpecial())
            {
                g2.setColor(new Color(255, 0, 0));
            }
            else
            {
                g2.setColor(new Color(0, 0, 0));
            }
            g2.drawRect(s.getX() * c_squareSize + 1, s.getY() * c_squareSize + 1, 28, 28);
        }
        else
        {
            g2.fillOval(s.getX() * c_squareSize + 1, s.getY() * c_squareSize + 1, 28, 28);
//            if (_model._opposingTeam.isAPlayer(s))
            {
                g2.setColor(new Color(240, 0, 0));
            }
   //         else
            {
                g2.setColor(new Color(0, 0, 0));
            }
            g2.drawOval(s.getX() * c_squareSize + 1, s.getY() * c_squareSize + 1, 28, 28);
        }

        g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 1.0f));
    }

    void paintZones(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        /*
         * Draw zones
         */
        if (_model != null)
        {
           /* dSquareCollection squares = _model._squares;
            if (squares != null)
            {
                for (int i_x = 0; i_x < squares.getNbX(); i_x++)
                {
                    for (int i_y = 0; i_y < squares.getNbY(); i_y++)
                    {
                        dSquare s = squares.getSquare(i_x,i_y);
                        paintZone(s, g);
                    }
                }
            }*/
        }
    }

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        super.paintComponent(g);

        if (_model != null)
        {
            paintMeteo(g);
            paintTeamLogo(g);
            paintPassRange(g);
/*            paintTeam(g, _model.getLeftTeam());
            paintTeam(g, _model.getRightTeam());*/
            paintZones(g);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
